/**
 * @param {number[][]} grid
 * @param {number[][]} moveCost
 * @return {number}
 */
 var minPathCost = function(grid, moveCost) {
  const n = grid.length, m = grid[0].length
  const dp = new Array(n).fill(0).map(() => new Array(m).fill(Infinity))
  for(let i = 0; i < m; i++) {
    dp[0][i] = grid[0][i]
  }
  for(let i = 1; i<n; i++) {
    for(let j = 0; j<m; j++) {
      for(let k = 0; k<m; k++) {
        dp[i][j] = Math.min(dp[i][j], grid[i][j] + dp[i-1][k] + moveCost[grid[i-1][k]][j])
      }
    }
  }
  return Math.min(...dp[n-1])
};